import org.junit.Test;
import org.springframework.util.SerializationUtils;
import redis.clients.jedis.Jedis;
import system.pojo.User;

import java.util.Date;

public class Demo {
    @Test
    public void test1(){
        Jedis jedis = new Jedis("127.0.0.1",6379);

        String key = "user";
        User value = new User(1,"张三",new Date());

        byte[] byteKey = SerializationUtils.serialize(key);
        byte[] byteValue = SerializationUtils.serialize(value);

        jedis.set(byteKey,byteValue);

        jedis.close();
    }

    @Test
    public void getByteArray(){
        //1.连接redis服务
        Jedis jedis = new Jedis("127.0.0.1",6379);
        //2.1 准备key
        String key = "user";
        //2.2 将key转换为byte[]
        byte[] byteKey = SerializationUtils.serialize(key);
        //2.3 jedis去redis中获取value
        byte[] values = jedis.get(byteKey);
        //2.4 将value反序列化为user对象
        User user = (User) SerializationUtils.deserialize(values);
        System.out.println("user"+user);

        //3.释放资源
        jedis.close();
    }
    @Test
    public void test3(){
        Jedis jedis = new Jedis("10.9.41.116", 6379);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            jedis.set("key"+i,"value"+i);
        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
        jedis.close();
    }
}
